package com.example.mpandroidchart1107;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.List;

public class Main3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);


        PieChart pieChart=findViewById(R.id.piechart);//实例化

        List<PieEntry> strings = new ArrayList<>();//调用饼图方法

        strings.add(new PieEntry(30f,"nb"));
        strings.add(new PieEntry(40f,"pls"));
        strings.add(new PieEntry(46f,"lc"));
        PieDataSet dataSet = new PieDataSet(strings,"票数");

        ArrayList<Integer> colors = new ArrayList<Integer>();

        colors.add(getResources().getColor(R.color.colorAccent));
        colors.add(getResources().getColor(R.color.colorPrimary));
        colors.add(getResources().getColor(R.color.colorPrimaryDark));
        dataSet.setColors(colors);

        PieData pieData = new PieData(dataSet);

        pieData.setDrawValues(true ); //把数字变成百分比  ValueFormatter（格式化数据值）
        pieData.setValueFormatter(new PercentFormatter());
        pieData.setValueTextSize(16f);//字体大小

        pieChart.setData(pieData);
        pieChart.invalidate();
        pieChart.setHoleRadius(0f);
        pieChart.setTransparentCircleRadius(0f);

        pieData.setValueTextSize(12f);
    }

}
